#include "dockwidget.h"
#include "ui_dockwidget.h"
#include <QString>

DockWidget::DockWidget(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::DockWidget)
{
    ui->setupUi(this);
    setWindowTitle("");

    ui->horizontalSlider->hide();
    ui->label_4->hide();
    this->hide();

    retrieverDatabase = QSqlDatabase::addDatabase("QSQLITE", "retrieverDB");
    retrieverDatabase.setDatabaseName("../config.db");
    if (!retrieverDatabase.open())
    {
        qDebug() << "retrieverDatabase failed to open. Reason:" << retrieverDatabase.lastError();
    }

    RetrieveAllConnectionNames();

}

DockWidget::~DockWidget()
{
    delete ui;
}

void DockWidget :: RetrieveAllConnectionNames()
{
    int row = 0;
    int col = 0;
    QSqlQuery retQuery(retrieverDatabase);


    int removerVar = (connectionNumber-1);
    while (removerVar >= 0)
    {
        ui->gridLayout->removeWidget(pushButtons[removerVar]);
        ui->gridLayout->removeWidget(labels[removerVar]);
        delete pushButtons[removerVar];
        delete labels[removerVar];

        removerVar--;
    }

    retQuery.exec("SELECT * FROM connections;");

    connectionNumber = 0;
this->hide();
    while (retQuery.next())
    {
        this->show();
        if (connectionNumber >= 99)
        {
            qDebug() << "Sidedock connection list is full. Delete some connections.";
            break;
        }
        col = 0;
        connection_names[connectionNumber] = retQuery.value(0).toString();
        pushButtons[connectionNumber] = new QPushButton();
        labels[connectionNumber] = new QLabel();
        if (activeConnections.contains(connection_names[connectionNumber]))
        {
            pushButtons[connectionNumber]->setText("Disconnect");
            labels[connectionNumber]->setText("<FONT COLOR=\"GREEN\">" + connection_names[connectionNumber] + "</FONT>");
        }
        else
        {
            pushButtons[connectionNumber]->setText("Connect");
            labels[connectionNumber]->setText("<FONT COLOR=\"RED\">" + connection_names[connectionNumber] + "</FONT>");
        }
        ui->gridLayout->addWidget(pushButtons[connectionNumber], row, col);
        col++;
        ui->gridLayout->addWidget(labels[connectionNumber], row, col);
        row++;

        connect(pushButtons[connectionNumber], SIGNAL(pressed()), this, SLOT(ConnectIsPressed()));

        connectionNumber++;
    }
}

void DockWidget::ConnectIsPressed()
{
    int counterVar = 0;

    while (counterVar < connectionNumber)
    {
        if (pushButtons[counterVar]->isDown())
        {
            if (pushButtons[counterVar]->text() == "Connect")
            {   // DO ALL THE STUFF THAT SHOULD BE DONE TO CONNECT.
                labels[counterVar]->setText("<FONT COLOR=\"GREEN\">" + connection_names[counterVar] + "</FONT>");

                activeConnections.append(connection_names[counterVar]);
                emit connectPushed(connection_names[counterVar]);
                qDebug() << "Connect to " << connection_names[counterVar];
                pushButtons[counterVar]->setText("Disconnect");

            }
            else
            {
                // DO ALL THE STUFF TO DISCONNECT.
                labels[counterVar]->setText("<FONT COLOR=\"RED\">" + connection_names[counterVar] + "</FONT>");

                int i = activeConnections.indexOf(connection_names[counterVar]);
                if (i >= 0)
                    activeConnections.removeAt(i);

                emit disconnectPushed(connection_names[counterVar]);
                pushButtons[counterVar]->setText("Connect");
            }
        }

        counterVar++;
    }
}

int DockWidget::getSliderValue(){

   return this->ui->horizontalSlider->value();

}
